package com.fr.report.core.barcode;

import com.fr.base.core.GraphHelper;
import com.fr.report.core.barcode.env.Environment;
import com.fr.report.core.barcode.env.EnvironmentFactory;
import com.fr.report.core.barcode.output.AbstractOutput;
import com.fr.report.core.barcode.output.GraphicsOutput;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.text.StringCharacterIterator;

public abstract class BarcodeImpl
{
  private static final String[] UNITS = { "in", "px", "cm", "mm" };
  private static final double DEFAULT_BAR_HEIGHT = 50.0D;
  protected String data;
  protected boolean drawingText;
  protected double barWidth = 1.0D;
  protected double barHeight;
  private Font font;
  private Color foreground;
  private Color background;
  private Dimension size;
  private int x;
  private int y;
  private int resolution = -1;

  protected BarcodeImpl(String paramString)
    throws BarcodeException
  {
    if ((paramString == null) || (paramString.length() == 0))
      throw new BarcodeException("Data to encode cannot be empty");
    this.data = paramString;
    double d = calculateMinimumBarHeight(getResolution());
    if (d > 0.0D)
      this.barHeight = d;
    else
      this.barHeight = 50.0D;
    this.font = EnvironmentFactory.getEnvironment().getDefaultFont();
    this.drawingText = true;
    setBackground(Color.white);
    setForeground(Color.black);
    setSVGScalar(0.0078125D, "in");
    invalidateSize();
  }

  public String getData()
  {
    return this.data;
  }

  public Font getFont()
  {
    return this.font;
  }

  public void setFont(Font paramFont)
  {
    this.font = paramFont;
    invalidateSize();
  }

  public Color getForeground()
  {
    return this.foreground;
  }

  public void setForeground(Color paramColor)
  {
    this.foreground = paramColor;
  }

  public Color getBackground()
  {
    return this.background;
  }

  public void setBackground(Color paramColor)
  {
    this.background = paramColor;
  }

  public void setDrawingText(boolean paramBoolean)
  {
    this.drawingText = paramBoolean;
    invalidateSize();
  }

  public boolean isDrawingText()
  {
    return this.drawingText;
  }

  public void setBarWidth(double paramDouble)
  {
    if (paramDouble >= 1.0D)
      this.barWidth = paramDouble;
    else
      this.barWidth = 1.0D;
    invalidateSize();
  }

  public void setBarHeight(double paramDouble)
  {
    if (paramDouble > calculateMinimumBarHeight(getResolution()))
    {
      this.barHeight = paramDouble;
      invalidateSize();
    }
  }

  public void setResolution(int paramInt)
  {
    if (paramInt > 0)
    {
      this.resolution = paramInt;
      double d = calculateMinimumBarHeight(getResolution());
      if (d > this.barHeight)
        this.barHeight = d;
      invalidateSize();
    }
  }

  public int getX()
  {
    return this.x;
  }

  public int getY()
  {
    return this.y;
  }

  public int getWidth()
  {
    return (int)getActualSize().getWidth();
  }

  public int getHeight()
  {
    return (int)getActualSize().getHeight();
  }

  public Rectangle getBounds()
  {
    return getBounds(new Rectangle());
  }

  public Rectangle getBounds(Rectangle paramRectangle)
  {
    paramRectangle.setBounds(getX(), getY(), (int)getActualSize().getWidth() + getX(), (int)getActualSize().getHeight() + getY());
    return paramRectangle;
  }

  public Dimension getPreferredSize()
  {
    return getActualSize();
  }

  public Dimension getMinimumSize()
  {
    return getActualSize();
  }

  public Dimension getMaximumSize()
  {
    return getActualSize();
  }

  public Dimension getSize()
  {
    return getActualSize();
  }

  public void draw(Graphics2D paramGraphics2D, int paramInt1, int paramInt2)
  {
    this.x = paramInt1;
    this.y = paramInt2;
    this.size = internalDraw(paramGraphics2D, paramInt1, paramInt2, true);
  }

  protected abstract double getBarcodeWidth(int paramInt);

  protected abstract Module[] encodeData();

  protected abstract Module calculateChecksum();

  protected abstract Module getPreAmble();

  protected abstract Module getPostAmble();

  protected String getLabel()
  {
    return this.data;
  }

  protected double calculateMinimumBarHeight(int paramInt)
  {
    return 0.0D;
  }

  protected int getResolution()
  {
    if (this.resolution > 0)
      return this.resolution;
    return EnvironmentFactory.getEnvironment().getResolution();
  }

  private Dimension internalDraw(Graphics2D paramGraphics2D, int paramInt1, int paramInt2, boolean paramBoolean)
  {
    Color localColor = paramGraphics2D.getColor();
    double d1 = paramInt1;
    GraphicsOutput localGraphicsOutput = new GraphicsOutput(paramGraphics2D, this.font, this.barWidth, this.barHeight, paramBoolean, getForeground(), getBackground());
    if (paramBoolean)
    {
      localObject = getActualSize();
      localGraphicsOutput.beginDraw(((Dimension)localObject).width, ((Dimension)localObject).height);
    }
    d1 += drawModule(getPreAmble(), localGraphicsOutput, d1, paramInt2);
    Object localObject = encodeData();
    for (int i = 0; i < localObject.length; ++i)
    {
      Module localModule = localObject[i];
      d1 += drawModule(localModule, localGraphicsOutput, d1, paramInt2);
    }
    d1 += drawModule(calculateChecksum(), localGraphicsOutput, d1, paramInt2);
    d1 += drawModule(getPostAmble(), localGraphicsOutput, d1, paramInt2);
    double d2 = this.barHeight + paramInt2;
    if (this.drawingText)
      d2 += drawTextLabel(localGraphicsOutput, paramInt1, (int)d2, d1);
    if (paramBoolean)
      localGraphicsOutput.endDraw();
    paramGraphics2D.setColor(localColor);
    return ((Dimension)new Dimension((int)(d1 - paramInt1), (int)d2 - paramInt2));
  }

  private void invalidateSize()
  {
    this.size = null;
  }

  private Dimension getActualSize()
  {
    if (this.size == null)
      this.size = calculateSize();
    return this.size;
  }

  private Dimension calculateSize()
  {
    return internalDraw((Graphics2D)GraphHelper.createBufferedImage(1000, 1000, 10).getGraphics(), 0, 0, false);
  }

  protected double drawModule(Module paramModule, AbstractOutput paramAbstractOutput, double paramDouble1, double paramDouble2)
  {
    if (paramModule == null)
      return 0.0D;
    return paramModule.draw(paramAbstractOutput, paramDouble1, paramDouble2);
  }

  protected String beautify(String paramString)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    StringCharacterIterator localStringCharacterIterator = new StringCharacterIterator(paramString);
    int i = localStringCharacterIterator.first();
    while (i != 65535)
    {
      if ((Character.isDefined(i)) && (!(Character.isISOControl(i))))
        localStringBuffer.append(i);
      int j = localStringCharacterIterator.next();
    }
    return localStringBuffer.toString();
  }

  public void setSVGScalar(double paramDouble, String paramString)
  {
    String str = "";
    int i = 0;
    for (int j = 0; (i == 0) && (j < UNITS.length); ++j)
    {
      if (paramString.equals(UNITS[j]))
        i = 1;
      if (j != 0)
        str = str + ", ";
      str = str + UNITS[j];
    }
    if (i == 0)
      throw new IllegalArgumentException("SVG Units must be one of " + str);
  }

  private double drawTextLabel(AbstractOutput paramAbstractOutput, double paramDouble1, double paramDouble2, double paramDouble3)
  {
    String str = beautify(getLabel());
    return paramAbstractOutput.drawText(str, paramDouble1, paramDouble2, paramDouble3);
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    return super.clone();
  }
}